<!-- 围栏地图页 -->
<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;color: #000;">新增围栏</span>
    <el-button type="primary" style="float:right;margin-top:13px;"
               @click="router.push('/geofence')">返回</el-button>
  </div>

  <!-- 第2步:准备一个用来装地图的容器 -->
  <div id="mapContainer" style="height: 84vh;width: 100%;"></div>

  <!-- 新增电子围栏弹窗 -->
  <el-dialog title="围栏信息" v-model="dialogVisible" :before-close="closeDialog">
    <el-form label-width="80px">
      <el-form-item label="围栏名称">
        <el-input placeholder="请输入围栏名称" v-model="geofence.name"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="closeDialog">取消</el-button>
      <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>
  </el-dialog>

</template>

<script setup>
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import { DrawScene, DrawControl , OperateEventType} from 'bmap-draw';
import qs from "qs";
import axios from "axios";

//定义变量控制弹窗是否出现
const dialogVisible = ref(false);

//定义对象用来保存围栏数据`
const geofence = ref({
  name:'',//围栏名称
  position:{}//围栏位置信息
});

var map;
onMounted(()=>{
  nextTick(()=>{
    map = new BMapGL.Map("mapContainer");
    var point = new BMapGL.Point(106.5179,29.5338);
    map.centerAndZoom(point,15);
    const scene = new DrawScene(map);
    const drawControl = new DrawControl(scene,{
      enableTips:false,
      anchor:BMAP_ANCHOR_TOP_RIGHT,
      drawingItems:[
          'circle',
          'rectangle'
      ]
    });

    scene.addEventListener(OperateEventType.COMPLETE,(event)=>{
      dialogVisible.value = true;
      let overlay = event.target.overlay;
      if (overlay instanceof BMapGL.Circle){//圆形
        geofence.value.position.type = 'circle';
        geofence.value.position.radius = overlay.getRadius();
        geofence.value.position.longitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
      }else {//矩形
        let recPoints = [];
        for (var i=0;i<4;i++){
          recPoints.push(overlay.points[i].latLng.lng + '-' + overlay.points[i].latLng.lat);
        }
        geofence.value.position.type = 'rectangle';
        geofence.value.position.recPoints = recPoints.join(',');
      }
    })
    map.addControl(drawControl);
  })
})

//定义新增围栏的方法
const saveGeofence = () => {
  if(!geofence.value.name || geofence.value.name.trim().length==0){
    ElMessage.error('围栏名称不能为空！');
    return;
  }
  geofence.value.position = JSON.stringify(geofence.value.position);
  let data = qs.stringify(geofence.value);
  axios.post(BASE_URL+'/v1/geofence/save',data)
      .then((response)=>{
        if (response.data.code == 2000){
          ElMessage.success('围栏新增成功！');
          dialogVisible.value = false;
          router.push('geofence');
        }else {
          ElMessage.error(response.data.msg);
        }
      })
};
//定义关闭弹窗的方法
const closeDialog = () => {
  if(confirm('确认关闭围栏吗?')){
    dialogVisible.value = false;
    map.clearOverlays();
    geofence.value = {name:'',position: {}};
  }
};
</script>

<style scoped>

</style>